package com.songcha.library_common.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.songcha.library_common.R
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.dp2px

abstract class BaseDialogFragment: DialogFragment() {
    private var mWidth:Int? = null
    private var mHeight:Int? = null
    private var mGravity:Int? = null
    private var mAnim:Int = R.style.dialogScaleWindowAnim

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = LayoutInflater.from(context).inflate(getLayoutId(),container,false)
        initView(view)
        return view
        //return super.onCreateView(inflater, container, savedInstanceState)
    }

    abstract fun initView(view:View)

    fun isShowing():Boolean{
        return dialog!=null && dialog!!.isShowing
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val window = dialog?.window
        window?.attributes?.apply {
            if(mWidth!=null)
                width = mWidth!!
            if(mHeight!=null)
                height = mHeight!!
            alpha = 1f
        }

        window?.apply {
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            if(mGravity!=null)
                setGravity(mGravity!!)
            setWindowAnimations(mAnim)
            //setDimAmount(mDimAmount)*/
        }
    }

    fun show(manager: FragmentManager){
        super.show(manager, this::class.java.simpleName)
    }

    abstract fun getLayoutId():Int

    fun setWidth(width:Int){
        mWidth = width
    }

    fun setHeight(height:Int){
        mHeight = height
    }

    fun setGravity(gravity:Int){
        mGravity = gravity
    }

    fun setAnim(anim:Int){
        mAnim = anim
    }

}